package com.spring.boot.standard.admin.common.beans;


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
    private static final int minutesOfseconds = 60;
    private static final int hourOfseconds = 3600;
    private static final int dayOfseconds = 86400;
    private static final ZoneOffset defaultZoneOffset = OffsetDateTime.now().getOffset();
    private static final ZoneId defaultZone = ZoneId.systemDefault();

    public DateUtil() {
    }

    public static final int getCurrentTime() {
        return Integer.parseInt(String.valueOf(System.currentTimeMillis() / 1000L));
    }

    public static final int getTime(Date dateTime) {
        return Integer.parseInt(String.valueOf(dateTime.getTime() / 1000L));
    }

    public static final String calculateTime(int startSeconds, int endSeconds) {
        int time = Math.abs(endSeconds - startSeconds);
        return calculateTime(time);
    }

    public static final String calculateTime(int seconds) {
        StringBuffer stringBuffer = new StringBuffer();
        int minutes;
        if (seconds >= 86400) {
            minutes = seconds / 86400;
            stringBuffer.append(minutes + "天");
            seconds %= 86400;
        }

        if (seconds >= 3600) {
            minutes = seconds / 3600;
            stringBuffer.append(minutes + "小时");
            seconds %= 3600;
        }

        if (seconds >= 60) {
            minutes = seconds / 60;
            stringBuffer.append(minutes + "分");
            seconds %= 60;
        }

        stringBuffer.append(seconds + "秒");
        return stringBuffer.toString();
    }

    public static final Date getTimeBeforeOfDay(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(14, 0);
        calendar.add(5, 0 - day);
        return calendar.getTime();
    }

    public static final String getCurrentYearMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(new Date());
    }

    public static int getTime(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return getTime(sdf.parse(dateStr));
        } catch (Exception var3) {
            var3.printStackTrace();
            return -1;
        }
    }

    public static Date format(int seconds) {
        return new Date(Long.valueOf(seconds + "000"));
    }

    public static LocalDateTime formatLocalDateTime(int seconds) {
        return LocalDateTime.ofInstant(format(seconds).toInstant(), defaultZone);
    }

    public static LocalDateTime formatLocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), defaultZone);
    }

    public static int getStartTimeOfMonth(Date date) {
        LocalDateTime localDateTime = formatLocalDateTime(date);
        return getStartTime(localDateTime.getYear(), localDateTime.getMonthValue());
    }

    public static int getStartTimeOfDay(Date date) {
        LocalDateTime localDateTime = formatLocalDateTime(date);
        return getStartTime(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth());
    }

    public static int getEndTimeOfMonth(Date date) {
        LocalDateTime localDateTime = formatLocalDateTime(date);
        return getEndTime(localDateTime.getYear(), localDateTime.getMonthValue());
    }

    public static int getEndTimeOfDay(Date date) {
        LocalDateTime localDateTime = formatLocalDateTime(date);
        return getEndTime(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth());
    }

    public static String format(int seconds, String format) {
        if (format == null || "".equals(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(format(seconds));
    }

    public static Date format(String dateStr, String format) throws Exception {
        if (format == null || "".equals(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateStr);
    }

    public static String format(Date date, String format) {
        if (format == null || "".equals(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static int getStartTime(int year, int month) {
        LocalDate firstday = LocalDate.of(year, month, 1).with(TemporalAdjusters.firstDayOfMonth());
        LocalTime localTime = LocalTime.of(0, 0, 0);
        LocalDateTime localDateTime = LocalDateTime.of(firstday, localTime);
        return Long.valueOf(localDateTime.toEpochSecond(defaultZoneOffset)).intValue();
    }

    public static int getStartTime(int year, int month, int day) {
        LocalDate firstday = LocalDate.of(year, month, day);
        LocalTime localTime = LocalTime.of(0, 0, 0);
        LocalDateTime localDateTime = LocalDateTime.of(firstday, localTime);
        return Long.valueOf(localDateTime.toEpochSecond(defaultZoneOffset)).intValue();
    }

    public static int getEndTime(int year, int month) {
        LocalDate endDay = LocalDate.of(year, month, 1).with(TemporalAdjusters.lastDayOfMonth());
        LocalTime localTime = LocalTime.of(23, 59, 59);
        LocalDateTime localDateTime = LocalDateTime.of(endDay, localTime);
        return Long.valueOf(localDateTime.toEpochSecond(defaultZoneOffset)).intValue();
    }

    public static int getEndTime(int year, int month, int day) {
        LocalDate endDay = LocalDate.of(year, month, day);
        LocalTime localTime = LocalTime.of(23, 59, 59);
        LocalDateTime localDateTime = LocalDateTime.of(endDay, localTime);
        return Long.valueOf(localDateTime.toEpochSecond(defaultZoneOffset)).intValue();
    }

    //获取日期的第一天和最后一天
    public  static List<String> getMonthStartEnd(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(date);
        calendarStart.set(Calendar.DAY_OF_MONTH,1);
        calendarStart.add(Calendar.MONTH,0);
        String startTime = format.format(calendarStart.getTime());
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(date);
        calendarEnd.set(Calendar.DAY_OF_MONTH,0);
        calendarEnd.add(Calendar.MONTH,1);
        String endTime = format.format(calendarEnd.getTime());
        List<String> result = new ArrayList<>();
        result.add(startTime);
        result.add(endTime);
        return result;
    }
    //获取日期的第一天和最后一天
    public  static List<String> getYearStartEnd(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(date);
        int year = calendarStart.get(Calendar.YEAR);
        String startTime = format.format(getYearFirst(year));
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(date);
        calendarEnd.set(Calendar.DAY_OF_MONTH,0);
        calendarEnd.add(Calendar.MONTH,1);
        String endTime = format.format(calendarEnd.getTime());
        List<String> result = new ArrayList<>();
        result.add(startTime);
        result.add(endTime);
        return result;
    }
    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }
}
